package com.example.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.example.demo.utils.Result;

import java.io.File;

@RestController
@Slf4j
public class SapUploadFileController {

    private final Logger log = LoggerFactory.getLogger(SapUploadFileController.class);
    @Autowired
    RestTemplate restTemplate;

    @Value("${restUploadURL}")
    private String restUploadURL;

    @Value("${SapfilePath}")
    private String SapfilePath;

    //private final String SapfilePath =filePath+"SAP Data"+"/";
    //@GetMapping("/uploadSalePerson")
  @Scheduled(cron = "0 30 9 * * ?")
    public void uploadSalePerson() {
        File file = new File(SapfilePath + "/" + "04_MSD-SO_SalePerson.txt");
        uploadFile(file);
    }

    //@GetMapping("/uploadOpenSO")
  @Scheduled(cron = "0 30 9 * * ?")
    public void uploadOpenSO() {
        File file = new File(SapfilePath + "/" + "01_MSD_OpenSO.txt");
        uploadFile(file);
    }

    //@GetMapping("/uploadDN")
  @Scheduled(cron = "0 30 9 * * ?")
    public void uploadDN() {
        File file = new File(SapfilePath + "/" + "02_MSD-DN.txt");
        uploadFile(file);
    }

    //@GetMapping("/uploadAPContact")
  @Scheduled(cron = "0 30 9 * * ?")
    public void uploadShipto() {
        File file = new File(SapfilePath + "/" + "05_MSD-SO_Shipto.txt");
        uploadFile(file);
    }

  @Scheduled(cron = "0 30 9 * * ?")
    public void uploadAPContact() {
        File file = new File(SapfilePath + "/" + "03_MSD-SO_APContact.txt");
        uploadFile(file);
    }

  @Scheduled(cron = "0 30 9 * * ?")
    public void uploadSoCsr() {
        File file = new File(SapfilePath + "/" + "06_MSD-SO_CSR.txt");
        uploadFile(file);
    }


    public void uploadFile(File file) {
        if (null != file && file.exists()) {
            try {
                log.info("=====file=====" + file.getName() + "==" + file.getPath() +
                        "==restUploadURL==" + restUploadURL);
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("multipart/form-data");
                headers.setContentType(type);
                FileSystemResource resource = new FileSystemResource(file);
                MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
                param.add("file", resource);
                HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
                ResponseEntity<Result> responseEntity =
                        restTemplate.postForEntity(restUploadURL, httpEntity, Result.class);
                Result response = responseEntity.getBody();
                log.info("=========response===========" + response.toString());
            } catch (Exception e) {
                log.error("Failed to upload file :" + e.getMessage());
            }
            log.info("End to upload file");
        } else {
            log.info("=====file=====" + file.getName() + "==" + file.getPath() + "not exists");
        }
    }


}
